package com.woniu.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.hutool.core.io.FileTypeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.dto.UserInfoDTO;
import com.woniu.dto.UserWorkerDetailDTO;
import com.woniu.exception.XinanException;
import com.woniu.exception.user.UserException;
import com.woniu.meals.client.MealsFeignClient;
import com.woniu.meals.dto.AddressDto;
import com.woniu.param.SelectUserInfoParam;
import com.woniu.param.UpdateUserInfoParam;
import com.woniu.user.entity.AreaCode;
import com.woniu.param.UpdateUserStuInfoParam;
import com.woniu.user.entity.UserInfo;
import com.woniu.user.mapper.UserInfoMapper;
import com.woniu.user.service.AreaCodeService;
import com.woniu.user.service.UserInfoService;
import com.woniu.utils.EnumException;
import com.woniu.utils.OSSUtil;

import com.woniu.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.stylesheets.LinkStyle;

import javax.annotation.Resource;
import java.util.List;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import java.time.LocalDateTime;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author ly
 * @since 2024年12月05日
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Resource
    AreaCodeService areaCodeService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Resource
    MealsFeignClient mealsFeignClient;


    @Override
    public void updateUserStoreInfo(Integer userId, Integer userPhone, MultipartFile userImag) throws Exception {
        // 处理文件上传
        String imageURL = null;
        if (userImag != null) {
            // 判断文件格式
            String type = FileTypeUtil.getType(userImag.getInputStream());
            if (!type.equals("jpg") && !type.equals("png") && !type.equals("pdf")) {
                throw new Exception("文件格式错误");
            }

            // 给文件改名UUID
            String filename = userImag.getOriginalFilename();
            String substring = filename.substring(filename.lastIndexOf("."));
            String realFilename = UUID.randomUUID().toString() + substring;

            // 上传文件
            imageURL = OSSUtil.uploadFile(userImag.getInputStream(), realFilename, "woniu108lanyu");
        }
        UserInfo userInfo = userInfoMapper.selectUserInfoByUserId(userId);
        userInfo.setUserPhone(userPhone.toString());
        userInfo.setUserImg(imageURL);

        userInfoMapper.updateUserInfoByUserId(userInfo);
    }

    @Override
    public void updateUserStuInfo(UpdateUserStuInfoParam updateUserStuInfoParam) throws Exception {

        System.out.println(updateUserStuInfoParam);
        UserInfo userInfo = userInfoMapper.selectUserInfoByUserId(Integer.valueOf(updateUserStuInfoParam.getUserId()));

        userInfo.setUserPhone(updateUserStuInfoParam.getUserPhone());
        userInfo.setAreaCodeName(areaCodeService.getById(updateUserStuInfoParam.getAreaCodeId()).getAreaCodeName());
        userInfo.setUserAddress(updateUserStuInfoParam.getUserAddress());
        userInfo.setUserAddressId(updateUserStuInfoParam.getUserAddressId());
        // 通过地址id找地址（feign）
        userInfo.setAreaCodeId(updateUserStuInfoParam.getAreaCodeId());
        userInfo.setAreaCodeName(areaCodeService.getById(updateUserStuInfoParam.getAreaCodeId()).getAreaCodeName());
        userInfo.setUserAddressId(updateUserStuInfoParam.getUserAddressId());
        Result<List<AddressDto>> listResult = mealsFeignClient.selectAddress(Integer.valueOf(updateUserStuInfoParam.getUserId()));
        System.out.println(listResult);
        String addressName = null;
        List<AddressDto> addressDtos = listResult.getData();
        for (AddressDto addressDto : addressDtos) {
            System.out.println(addressDto.getAddressId());
            System.out.println(updateUserStuInfoParam.getUserAddressId());
            if (addressDto.getAddressId().toString().equals(updateUserStuInfoParam.getUserAddressId().toString())) {
                System.out.println("1111111111111111");
                addressName = addressDto.getAddressName();
            }
        }
//        String addressName = addressDtos.stream()
//                .filter(addressDto -> addressDto.getAddressId().equals(updateUserStuInfoParam.getUserAddressId()))
//                .map(AddressDto::getAddressName)
//                        .findFirst().get();
        System.out.println("+++++++++++++++++" + addressDtos.size());
        System.out.println("+++++++++++++++++" + addressName);
        userInfo.setUserAddressInfo(addressName);

        this.updateById(userInfo);
    }


    @Override
    public void changeDefaultAddress(Integer userId, Integer addressId, String addressName) {
        UserInfo userInfo = userInfoMapper.selectUserInfoByUserId(userId);
        userInfo.setUserAddressId(addressId.toString());
        userInfo.setUserAddressInfo(addressName);
        this.updateById(userInfo);
    }



}
